﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThaiHoa.Win.QLSX.Entity;
using ThaiHoa.Win.QLSX.Kho.UTI;
using ThaiHoa.Win.QLSX.DAL;
using Excel = Microsoft.Office.Interop.Excel;

namespace ThaiHoa.Win.QLSX.Kho.UI
{
    public partial class frmDonViTinhList : Form
    {
        #region Fields
        KhoDBContext db;

        BindingSource DonViTinhBS;
        Access acs;

        #endregion
        #region Constructor
        public frmDonViTinhList()
        {
            InitializeComponent();
        }
        #endregion
        #region Functions
        private void KhoiTao()
        {
            db = new KhoDBContext();
            db.Saved += new EventHandler(db_Saved);
            DonViTinhBS = new BindingSource();
            acs = new Access();
            db.DonViTinh.Where(m => m.TrangThai).Load();
            DonViTinhBS.DataSource = db.DonViTinh.Local.ToBindingList();
            dgvDonViTinh.DataSource = DonViTinhBS;
            DonViTinhBN.BindingSource = DonViTinhBS;
            CustomerGridView();
        }

        private void ReLoadData()
        {
            if (db != null) db.Dispose();
            KhoiTao();
        }

        private void CustomerGridView()
        {
            //Ẩn toàn bộ columns
            foreach (DataGridViewColumn item in dgvDonViTinh.Columns)
            {
                item.Visible = false;
            }
            //Khóa tính năng thêm mới
            dgvDonViTinh.AllowUserToAddRows = false;
            //Tùy chỉnh lại

            //Tên
            var c = dgvDonViTinh.Columns["Ten"];
            c.Visible = true;
            c.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            c.DisplayIndex = 2;

        }

        private void HienThiChiTiet()
        {
            DonViTinh item = DonViTinhBS.Current as DonViTinh;
            if (item != null)
            {
                frmDonViTinhDetail _frmDonViTinhDetail = new frmDonViTinhDetail();
                _frmDonViTinhDetail.DBChanged += new EventHandler(_frmDonViTinhDetail_DBChanged);
                acs.ShowFormDonViTinhDetail(_frmDonViTinhDetail, item.Id);
            }
        }

        private void Xoa()
        {
            DonViTinh item = DonViTinhBS.Current as DonViTinh;
            if (Common.ShowMessengerConfirmDelete() == DialogResult.Yes)
            {
                if (item != null)
                {
                    db.DonViTinh.Remove(item);
                    db.SaveChanges();
                }
            }
        }

        private void LuuLai()
        {
            if (Common.ShowMessengerConfirmSave() == DialogResult.Yes)
            {
                db.SaveChanges();
            }
        }
        private void ExportToExcel()
        {
            //string workbookPath = Application.StartupPath + @"\Template\Excel\JAT_DonViTinh";
            //Excel.Application xlApp = new Excel.Application();
            //Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(workbookPath,
            //   0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "",
            //    true, false, 0, true, false, false);
            //Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
            //xlWorkBook.Activate();
            //xlWorkSheet.Name = "DonViTinh";
            ////Add data
            //var listDonViTinh = DonViTinhBS.List;
            //int count = 0;
            //int positonStart = 11;
            //Excel.Range r;
            //foreach (DonViTinh item in listDonViTinh)
            //{
            //    xlWorkSheet.Cells[positonStart + count, 1] = count + 1;
            //    xlWorkSheet.Cells[positonStart + count, 2] = item.TenLoai;
            //    xlWorkSheet.Cells[positonStart + count, 3] = item.MoTa;

            //    //Format it               

            //    string start = "A" + (count + positonStart).ToString();
            //    string stop = "E" + (count + positonStart).ToString();
            //    r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
            //    r.Borders.Color = Color.Black;
            //    r.RowHeight = 30;
            //    //r.Style.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
            //    r.BorderAround(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlThin, Excel.XlColorIndex.xlColorIndexAutomatic, Excel.XlColorIndex.xlColorIndexAutomatic);
            //    start = "C" + (count + positonStart).ToString();
            //    stop = "E" + (count + positonStart).ToString();
            //    r = (Excel.Range)xlWorkSheet.get_Range(start, stop);
            //    r.Merge();
            //    count++;


            //}
            //xlWorkSheet.Cells[7, 4] = string.Format("Ngày {0}/{1}/{2}", DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year);
            //// Hiện chương trình
            //xlApp.Visible = true;

            ////// Save file
            ////xlWorkBook.SaveAs("C:\\JAT_VatTu_DanhMucThepDac_1.xls", Excel.XlFileFormat.xlWorkbookNormal,
            ////                null, null, false, false,
            ////                Excel.XlSaveAsAccessMode.xlExclusive,
            ////                false, false, false, false, false);
            //xlApp.Quit();
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
            //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
        }
        #endregion
        #region Events

        private void frmDonViTinhList_Load(object sender, EventArgs e)
        {
            KhoiTao();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            frmDonViTinhDetail _frmDonViTinhDetail = new frmDonViTinhDetail();
            _frmDonViTinhDetail.DBChanged += new EventHandler(_frmDonViTinhDetail_DBChanged);
            acs.ShowFormDonViTinhDetail(_frmDonViTinhDetail, Guid.Empty);
        }



        private void btnInformation_Click(object sender, EventArgs e)
        {
            HienThiChiTiet();
        }
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            ReLoadData();
        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDonViTinh_DoubleClick(object sender, EventArgs e)
        {
            HienThiChiTiet();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            LuuLai();
        }

        void db_Saved(object sender, EventArgs e)
        {
            ReLoadData();
        }
        void _frmDonViTinhDetail_DBChanged(object sender, EventArgs e)
        {
            ReLoadData();
        }
        private void btnExport_Click(object sender, EventArgs e)
        {
            ExportToExcel();
        }
        #endregion


    }
}
